# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=libcerf
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.17
pkgrel=1
epoch=1
pkgdesc="Complex error functions, Dawson, Faddeeva, and Voigt function (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://jugit.fz-juelich.de/mlz/libcerf'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
            # "${MINGW_PACKAGE_PREFIX}-ninja") failed to run pod2man command
source=("https://jugit.fz-juelich.de/mlz/libcerf/-/archive/v${pkgver}/${_realname}-v${pkgver}.tar.gz"
        "libcerf-fix-installation-dirs.patch")
sha256sums=('b1916b292cb37f2d0d0b699fbcf0fe260cca97ec7266ea20ff0c5cd8ef2eaab4'
            '851f84b408f97f42c983d672ea16968263dc5de4007d3f13a6a677d319608e06')

prepare() {
  cd "${srcdir}/${_realname}-v${pkgver}"
  patch -Np1 -i "${srcdir}/libcerf-fix-installation-dirs.patch"
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=ON \
      ../${_realname}-v${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/ctest || true
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  # License
  install -Dm644 "${srcdir}/${_realname}-v${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
